package com.platform.qujia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.qujia.dao.SaleActivityMapper;
import com.platform.qujia.pojo.SaleActivity;
import com.platform.qujia.pojo.vo.SaleActivityListVO;
import com.platform.qujia.service.ISaleActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @date 2023/12/24
 */
@Service
public class SaleActivityServiceImpl extends ServiceImpl<SaleActivityMapper, SaleActivity> implements ISaleActivityService {

    @Override
    public SaleActivity calculateAmt(Long saleActivityId, Integer purchaseNum) {
        SaleActivity activity = getById(saleActivityId);
        Assert.isTrue(activity != null && activity.getActivityEnable() == 1,"活动不存在");
        SaleActivity saleActivity = new SaleActivity();
        BigDecimal pn = BigDecimal.valueOf(purchaseNum);
        saleActivity.setPrice(activity.getPrice().multiply(pn));
        if (activity.getOriginalPrice() != null) {
            saleActivity.setOriginalPrice(activity.getOriginalPrice().multiply(pn));
        }
        return saleActivity;
    }

    @Override
    public List<SaleActivityListVO> queryListWithoutEnded(Long enterpriseId) {
        LambdaQueryWrapper<SaleActivity> qw = new LambdaQueryWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        qw.eq(SaleActivity::getEnterpriseId,enterpriseId);
//                .eq(SaleActivity::getActivityEnable,1)
//                .gt(SaleActivity::getEndTime, now)
//                .orderByAsc(SaleActivity::getStartTime);
        List<SaleActivity> list = list(qw);
        return list.stream().map(activity -> {
            if (activity.getStartTime() != null && activity.getEndTime() != null) {
                SaleActivityListVO activityListVO = new SaleActivityListVO();
                BeanUtils.copyProperties(activity,activityListVO);
                if (now.isBefore(activity.getStartTime())) {
                    activityListVO.setActivityStatus(0);
                    Duration duration = Duration.between(now, activity.getStartTime());
                    activityListVO.setDistantToStart(duration.toMillis());
                }else if(now.isAfter(activity.getEndTime())){
                    activityListVO.setActivityStatus(2);
                }else{
                    activityListVO.setActivityStatus(1);
                    Duration duration = Duration.between(now, activity.getEndTime());
                    activityListVO.setDistantToEnd(duration.toMillis());
                }
                return activityListVO;
            }
            return null;
        }).filter(Objects::nonNull).sorted(this::compareActivity).collect(Collectors.toList());
    }

    @Override
    public int increaseStock(Long id, int incrStock) {
        return baseMapper.increaseStock(id,incrStock);
    }

    private int compareActivity(SaleActivityListVO a1,SaleActivityListVO a2){
        int i = Integer.compare(activityWight(a1), activityWight(a2));
        if (i==0) {
            return a2.getId().compareTo(a1.getId());
        }
        return i;
    }

    private int activityWight(SaleActivityListVO activity){
        if (activity.getActivityStatus() == 1 && activity.getActivityEnable() == 1) {
            return 0;
        }
        if (activity.getActivityEnable()==0) {
            return 1;
        }
        return activity.getActivityStatus() + 2;
    }

}
